package com.hulk.ratel.common.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @Author: hulk
 * @E-mail: 29572320@qq.com
 * @Date: Create in 2018/9/7 下午5:28
 * @Version: 1.0.0
 * @Modify by:
 * Class description
 */

public class GenerateRandomKey{




    /**
     * 生成128bit 16位密钥
     * @return
     */
    public static String generate16Key() {
        return generateRandomKey(16);
    }
    /**
     * 生成192bit 24位密钥
     * @return
     */
    public static String generate24Key() {
        return generateRandomKey(24);
    }
    /**
     * 生成256bit 32位密钥
     * @return
     */
    public static String generate32Key() {
        return generateRandomKey(32);
    }
    /**
     * 生成随机密钥
     *
     * @param size 位数
     * @return
     */
    public static String generateRandomKey(int size) {
        StringBuilder key = new StringBuilder();
        String chars = "0123456789ABCDEF";
        for (int i = 0; i < size; i++) {
            int index = (int) (Math.random() * (chars.length() - 1));
            key.append(chars.charAt(index));
        }
        return key.toString();
    }

}
